package com.beteng.su.betengsu.ui.fragment.task;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.beteng.su.betengsu.R;
import com.beteng.su.betengsu.bean.AreaBean;
import com.blankj.utilcode.util.LogUtils;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import butterknife.BindView;
import io.realm.Realm;
import io.realm.RealmResults;


public class AddressChooseActivity extends AppCompatActivity {

    public static final String ID_RESULT = "idResult";
    public static final String STR_RESULT = "strResult";
    public static final int RESULT_CODE = 1008;
    public static final String BUNDLE_2 = "bundle2";
    public static final String STR_RESULT_NAME = "strResultName";

    private ListView mListView1;
    private ListView mListView2;
    private ListView mListView3;

    private LinearLayout mLinearLayout1;
    private LinearLayout mLinearLayout2;
    private LinearLayout mLinearLayout3;

    private LinearLayout onBackLinear;
    private TextView topTxt;

    private List<String> sheng = new ArrayList<>();
    private List<String> shi = new ArrayList<String>();
    private List<String> qu = new ArrayList<String>();

    private ArrayAdapter<String> shengAdapter;
    private ArrayAdapter<String> shiAdapter;
    private ArrayAdapter<String> quAdapter;

    private TextView shengTxt2;
    private TextView shengTxt3, shiTxt3, topView3;

    private static String shengStr, shiStr, quStr;
    private int areaID;
    private int shengID;
    private AutoRelativeLayout mLayoutBack;
    private TextView mTvTitle;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.my_set_addresschoose);



        mLinearLayout1 = (LinearLayout) this.findViewById(R.id.my_set_adresschoose_1);
        mLinearLayout2 = (LinearLayout) this.findViewById(R.id.my_set_adresschoose_2);
        mLinearLayout3 = (LinearLayout) this.findViewById(R.id.my_set_adresschoose_3);
        mLayoutBack = (AutoRelativeLayout) this.findViewById(R.id.toolbar_back);
        mTvTitle = (TextView) this.findViewById(R.id.toolbar_title);
        mTvTitle.setText(getTitle());

        mLayoutBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        mLinearLayout1.setVisibility(View.VISIBLE);
        mLinearLayout2.setVisibility(View.GONE);
        mLinearLayout3.setVisibility(View.GONE);


        shengTxt2 = (TextView) this.findViewById(R.id.my_set_adresschoose_sheng_2);
        shengTxt3 = (TextView) this.findViewById(R.id.my_set_adresschoose_sheng_3);
        shiTxt3 = (TextView) this.findViewById(R.id.my_set_adresschoose_shi_3);
        topView3 = (TextView) this.findViewById(R.id.my_set_adresschoose_textview_3);

//		landDivideDB = LandDivideDB.getInstance(getBaseContext());

        RealmResults<AreaBean> areaBeen = Realm.getDefaultInstance().where(AreaBean.class)
                .equalTo("AreaTypeID",1)
                .equalTo("IsDeleted", false)
                .findAll();

        RealmResults<AreaBean> allBean = Realm.getDefaultInstance().where(AreaBean.class)
                .findAll();

        LogUtils.d(allBean.size());

        if (areaBeen != null) {
            for (int i = 0; i < areaBeen.size(); i++) {
                sheng.add(areaBeen.get(i).getName());
            }
        }


        shengAdapter = new ArrayAdapter(this, R.layout.my_set_addresschoose_listview_item, R.id.my_set_adresschoose_textview, sheng);
        shiAdapter = new ArrayAdapter(this, R.layout.my_set_addresschoose_listview_item, R.id.my_set_adresschoose_textview, shi);
        quAdapter = new ArrayAdapter(this, R.layout.my_set_addresschoose_listview_item, R.id.my_set_adresschoose_textview, qu);

        mListView1 = (ListView) this.findViewById(R.id.my_set_adresschoose_listview_1);
        mListView1.setAdapter(shengAdapter);
        mListView2 = (ListView) this.findViewById(R.id.my_set_adresschoose_listview_2);
        mListView2.setAdapter(shiAdapter);
        mListView3 = (ListView) this.findViewById(R.id.my_set_adresschoose_listview_3);
        mListView3.setAdapter(quAdapter);

        shengTxt2.setOnClickListener(click);
        shengTxt3.setOnClickListener(click);
        shiTxt3.setOnClickListener(click);

        mListView1.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                                    long arg3) {
                // TODO Auto-generated method stub
                mLinearLayout1.setVisibility(View.GONE);
                mLinearLayout2.setVisibility(View.VISIBLE);
                mLinearLayout3.setVisibility(View.GONE);

                shi.clear();
                String name = sheng.get(position);
                shengStr = name;
                shengTxt2.setText(name);

//				List<LandDivide> landDivide = landDivideDB.queryAddress("name=?", new String[]{name});


                AreaBean firstAreaBean = Realm.getDefaultInstance().where(AreaBean.class)
                        .equalTo("FullName", name)
                        .equalTo("IsDeleted", false)
                        .findFirst();


                if (firstAreaBean != null) {
                    shengID = firstAreaBean.getID();
                }


                RealmResults<AreaBean> firstAreaBean_2 = Realm.getDefaultInstance().where(AreaBean.class)
                        .equalTo("ParentID", shengID)
                        .equalTo("IsDeleted", false)
                        .findAll();


                if (firstAreaBean_2 != null) {
                    for (int i = 0; i < firstAreaBean_2.size(); i++) {
                        shi.add(firstAreaBean_2.get(i).getName());
                    }
                }


                shiAdapter.notifyDataSetChanged();
                quAdapter.clear();
                quAdapter.notifyDataSetChanged();
            }
        });

        mListView2.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                                    long arg3) {

                qu.clear();

                String name = shi.get(position);
                shiStr = name;
                shengTxt3.setText(shengStr);
                shiTxt3.setText(name);


                AreaBean firstBean = Realm.getDefaultInstance().where(AreaBean.class)
                        .equalTo("Name", name)
                        .equalTo("IsDeleted", false)
                        .findFirst();

                if (firstBean != null) {
                    areaID = firstBean.getID();
                }


                RealmResults<AreaBean> landDivide_2 = Realm.getDefaultInstance().where(AreaBean.class)
                        .equalTo("ParentID", areaID)
                        .equalTo("IsDeleted", false)
                        .findAll();


                if (landDivide_2 != null) {
                    Iterator<AreaBean> iterator_2 = landDivide_2.iterator();
                    while (iterator_2.hasNext()) {
                        AreaBean l = iterator_2.next();
                        qu.add(l.getName());
                    }
                }


                quAdapter.notifyDataSetChanged();

                if (qu.size() < 1) {
                    mLinearLayout1.setVisibility(View.GONE);
                    mLinearLayout2.setVisibility(View.VISIBLE);
                    mLinearLayout3.setVisibility(View.GONE);


                    Intent intent2 = new Intent();
                    Bundle bundle2 = new Bundle();
                    bundle2.putInt(ID_RESULT, areaID);
                    bundle2.putString(STR_RESULT, shengStr + shiStr);
                    intent2.putExtra(BUNDLE_2, bundle2);
                    setResult(RESULT_CODE, intent2);
                    finish();

                } else {

                    mLinearLayout1.setVisibility(View.GONE);
                    mLinearLayout2.setVisibility(View.GONE);
                    mLinearLayout3.setVisibility(View.VISIBLE);

                    mListView3.setVisibility(View.VISIBLE);
                    topView3.setText("请选择  县区/其他...");
                }
            }
        });

        mListView3.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                                    long arg3) {
                // TODO Auto-generated method stub

                String name = qu.get(position);

                quStr = name;
                String purposeName = "";
                AreaBean landDivide = new AreaBean();
                try {

                    landDivide = Realm.getDefaultInstance().where(AreaBean.class)
                            .equalTo("Name", name)
                            .equalTo("ParentID", areaID)
                            .equalTo("IsDeleted", false)
                            .findFirst();


                    if (landDivide != null) {
                        purposeName = landDivide.getPurposeName();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    purposeName = "";
                }


//				Toast.makeText(AddressChooseActivity.this, shengStr + " " + shiStr + " " + quStr, Toast.LENGTH_SHORT).show();
                Intent intent2 = new Intent();
                Bundle bundle2 = new Bundle();
                bundle2.putInt(ID_RESULT, areaID);
                bundle2.putString(STR_RESULT, shengStr + shiStr + quStr);
                bundle2.putString(STR_RESULT_NAME, purposeName);
                bundle2.putSerializable("h", landDivide);
                intent2.putExtra(BUNDLE_2, bundle2);
                setResult(RESULT_CODE, intent2);
                finish();
            }
        });

    }

    OnClickListener click = new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
                case R.id.my_set_adresschoose_sheng_2:
                    mLinearLayout1.setVisibility(View.VISIBLE);
                    mLinearLayout2.setVisibility(View.GONE);
                    mLinearLayout3.setVisibility(View.GONE);
                    break;
                case R.id.my_set_adresschoose_sheng_3:
                    mLinearLayout1.setVisibility(View.VISIBLE);
                    mLinearLayout2.setVisibility(View.GONE);
                    mLinearLayout3.setVisibility(View.GONE);
                    break;
                case R.id.my_set_adresschoose_shi_3:
                    mLinearLayout1.setVisibility(View.GONE);
                    mLinearLayout2.setVisibility(View.VISIBLE);
                    mLinearLayout3.setVisibility(View.GONE);
                    break;
                case R.id.toolbar_back:
                    finish();
                default:
                    break;
            }
        }
    };

}
